/*
 * jPOS' miniSpace
 * Copyright (C) 2000-2006 Alejandro Revilla
 * jPOS.org (http://jpos.org)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 * USA
 *
 */
/*
 * jPOS' miniSpace
 * Copyright (C) 2000-2006 Alejandro Revilla
 * jPOS.org (http://jpos.org)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 * USA
 *
 */
package org.jpos.util;

import java.io.PrintStream;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

/**
 * Allow runtime binding of jPOS's components (ISOChannels, Logger, MUXes, etc)
 * @author <a href="mailto:apr@cs.com.uy">Alejandro P. Revilla</a>
 * @version $Revision: 2316 $ $Date: 2006-05-16 17:31:11 -0300 (Tue, 16 May 2006) $
 */
public class NameRegistrar implements Loggeable {
    private static NameRegistrar instance = new NameRegistrar();
    private Map registrar;

    public static class NotFoundException extends Exception {

        private static final long serialVersionUID = 8744022794646381475L;
        public NotFoundException() {
            super();
        }
        public NotFoundException(String detail) {
            super(detail);
        }
    }

    private NameRegistrar() {
        super();
        registrar = new Hashtable();
    }
    public static Map getMap() {
        return getInstance().registrar;
    }
    /**
     * @return singleton instance
     */
    public static NameRegistrar getInstance() {
        return instance;
    }
    /**
     * register object
     * @param key - key with which the specified value is to be associated.
     * @param value - value to be associated with the specified key
     */
    public static void register (String key, Object value) {
        getMap().put (key, value);
    }
    /**
     * @param key key whose mapping is to be removed from registrar.
     */
    public static void unregister (String key) {
        getMap().remove (key);
    }
    /**
     * @param key key whose associated value is to be returned.
     * @throws NotFoundException if key not present in registrar
     */
    public static Object get (String key) throws NotFoundException {
        Object obj = getMap().get(key);
        if (obj == null)
            throw new NotFoundException (key);
        return obj;
    }

    public void dump (PrintStream p, String indent) {
        dump(p,indent,false);
    }
    public void dump (PrintStream p, String indent, boolean detail) {
        String inner = indent + "  ";
        p.println (indent + "<name-registrar>");
        Iterator iter = registrar.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next ();
            p.println (inner + 
                "<name>" + entry.getKey().toString() + "</name>"
            );
            Object obj = entry.getValue();
            p.println (inner + 
                "<class>" + obj.getClass().getName()+ "</class>"
            );
            if ((detail == true) && (obj instanceof Loggeable)) {
                p.println(inner + "  <detail>");
                ((Loggeable)obj).dump(p, inner+"    ");
                p.println(inner + "  </detail>");
            }
        }
        p.println (indent + "</name-registrar>");
    }
}

